package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataCurrencyListDTO;
import com.ctshk.rpc.system.dto.MainDataTeaPartyPlaceDTO;
import com.ctshk.rpc.system.dto.MainDataTeaPartyPlaceRoomDTO;
import com.ctshk.rpc.system.entity.MainDataTeaPartyPlace;
import com.ctshk.rpc.system.entity.MainDataTeaPartyPlaceRoom;
import com.ctshk.rpc.system.mapper.MainDataTeaPartyPlaceMapper;
import com.ctshk.rpc.system.mapper.MainDataTeaPartyPlaceRoomMapper;
import com.ctshk.rpc.system.req.MainDataTeaPartyPlaceIdReq;
import com.ctshk.rpc.system.req.MainDataTeaPartyPlaceListReq;
import com.ctshk.rpc.system.req.MainDataTeaPartyPlaceReq;
import com.ctshk.rpc.system.service.IMainDataTeaPartyPlaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 茶會地點表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-17
 */
@DubboService
public class MainDataTeaPartyPlaceServiceImpl extends ServiceImpl<MainDataTeaPartyPlaceMapper, MainDataTeaPartyPlace> implements IMainDataTeaPartyPlaceService {

    @Autowired
    private MainDataTeaPartyPlaceRoomMapper mainDataTeaPartyPlaceRoomMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(MainDataTeaPartyPlaceReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        long id = IdWorker.getId();
        MainDataTeaPartyPlace teaPartyPlace = EntityUtil.copy(req, MainDataTeaPartyPlace.class);
        teaPartyPlace.setIsDeleted(IsDeletedCode.NO.getCode());
        teaPartyPlace.setCreateId(tokenUser.getId());
        teaPartyPlace.setModifiedId(tokenUser.getId());
        teaPartyPlace.setModifiedName(tokenUser.getUsername());
        teaPartyPlace.setGmtCreate(now);
        teaPartyPlace.setGmtModified(now);
        teaPartyPlace.setId(id);
        baseMapper.insert(teaPartyPlace);
        List<MainDataTeaPartyPlaceRoom> teaPartyPlaceRooms = EntityUtil.copyList(req.getRoomList(), MainDataTeaPartyPlaceRoom.class);
        for (MainDataTeaPartyPlaceRoom teaPartyPlaceRoom:teaPartyPlaceRooms) {
            teaPartyPlaceRoom.setId(IdWorker.getId());
            teaPartyPlaceRoom.setTeaPartyPlaceId(id);
            teaPartyPlaceRoom.setGmtCreate(now);
            teaPartyPlaceRoom.setGmtModified(now);
            mainDataTeaPartyPlaceRoomMapper.insert(teaPartyPlaceRoom);
        }
        return Result.success(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(MainDataTeaPartyPlaceReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataTeaPartyPlace teaPartyPlace = EntityUtil.copy(req, MainDataTeaPartyPlace.class);
        teaPartyPlace.setModifiedName(tokenUser.getUsername());
        teaPartyPlace.setGmtModified(now);
        baseMapper.updateById(teaPartyPlace);
        QueryWrapper<MainDataTeaPartyPlaceRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataTeaPartyPlaceRoom::getTeaPartyPlaceId,req.getId());
        mainDataTeaPartyPlaceRoomMapper.delete(queryWrapper);
        List<MainDataTeaPartyPlaceRoom> teaPartyPlaceRooms = EntityUtil.copyList(req.getRoomList(), MainDataTeaPartyPlaceRoom.class);
        for (MainDataTeaPartyPlaceRoom teaPartyPlaceRoom:teaPartyPlaceRooms) {
            teaPartyPlaceRoom.setId(IdWorker.getId());
            teaPartyPlaceRoom.setTeaPartyPlaceId(req.getId());
            teaPartyPlaceRoom.setGmtCreate(now);
            teaPartyPlaceRoom.setGmtModified(now);
            mainDataTeaPartyPlaceRoomMapper.insert(teaPartyPlaceRoom);
        }
        return Result.success(req.getId());
    }

    @Override
    public MainDataTeaPartyPlaceDTO query(MainDataTeaPartyPlaceIdReq req) {
        MainDataTeaPartyPlace mainDataTeaPartyPlace = baseMapper.selectById(req.getId());
        MainDataTeaPartyPlaceDTO teaPartyPlaceDTO = EntityUtil.copy(mainDataTeaPartyPlace, MainDataTeaPartyPlaceDTO.class);
        QueryWrapper<MainDataTeaPartyPlaceRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataTeaPartyPlaceRoom::getTeaPartyPlaceId,req.getId());
        queryWrapper.lambda().orderByDesc(MainDataTeaPartyPlaceRoom::getTeaPartyPlaceRoom);
        List<MainDataTeaPartyPlaceRoom> mainDataTeaPartyPlaceRooms = mainDataTeaPartyPlaceRoomMapper.selectList(queryWrapper);
        List<MainDataTeaPartyPlaceRoomDTO> teaPartyPlaceRoomDTOS = EntityUtil.copyList(mainDataTeaPartyPlaceRooms, MainDataTeaPartyPlaceRoomDTO.class);
        teaPartyPlaceDTO.setTeaPartyPlaceRooms(teaPartyPlaceRoomDTOS);
        return teaPartyPlaceDTO;
    }

    @Override
    public PageResponse<MainDataTeaPartyPlaceDTO> list(MainDataTeaPartyPlaceListReq req) {
        Page<MainDataTeaPartyPlace> page = new Page<>();
        QueryWrapper<MainDataTeaPartyPlace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataTeaPartyPlace::getStatus,req.getStatus());
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getName()),MainDataTeaPartyPlace::getTeaPartyPlace,req.getName());
        queryWrapper.lambda().orderByDesc(MainDataTeaPartyPlace::getGmtModified);
        Page<MainDataTeaPartyPlace> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataTeaPartyPlace> records = iPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataTeaPartyPlaceDTO> mainDataTeaPartyPlaceDTOS = EntityUtil.copyList(records, MainDataTeaPartyPlaceDTO.class);
        for (MainDataTeaPartyPlaceDTO teaPartyPlaceDTO:mainDataTeaPartyPlaceDTOS) {
            QueryWrapper<MainDataTeaPartyPlaceRoom> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(MainDataTeaPartyPlaceRoom::getTeaPartyPlaceId,teaPartyPlaceDTO.getId());
            wrapper.lambda().orderByDesc(MainDataTeaPartyPlaceRoom::getTeaPartyPlaceRoom);
            List<MainDataTeaPartyPlaceRoom> mainDataTeaPartyPlaceRooms = mainDataTeaPartyPlaceRoomMapper.selectList(wrapper);
            List<MainDataTeaPartyPlaceRoomDTO> teaPartyPlaceRoomDTOS = EntityUtil.copyList(mainDataTeaPartyPlaceRooms, MainDataTeaPartyPlaceRoomDTO.class);
            teaPartyPlaceDTO.setTeaPartyPlaceRooms(teaPartyPlaceRoomDTOS);
        }
        return new PageResponse<>(mainDataTeaPartyPlaceDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }
}
